import { Action, ActionKey } from "@/action/Action";
import { MenuItem } from "../../comps/station/graphTabs/diagramTableNew/config/toolBar";
import { DiagramTableEditor } from "../../comps/station/graphTabs/diagramTableNew/DiagramTableEditor";
import { ImportExcelToTable } from '@/modules/model/modals/importExcelToTable/';
import { PropertyKey } from "@/constants/Enums";
import { StType } from "@/constants/StType";

export class TableImportExcelAction extends Action {
  desc="导入Excel"
  keyName = ActionKey.TableImportExcel

  async onTableToolbarClick(tableEditor: DiagramTableEditor, menuItem?:MenuItem) {
    console.log(this.desc);
    if (tableEditor.disabled) {
      return;
    }
    if (menuItem?.key == 'excel') {
      // 如果没有选择分类器，提示用户
      const classifiers = (tableEditor.propertyModel.propertyRows.find(it => it.key === PropertyKey.InstanceTable_classifiers)?.value || []) as string[];
      if (tableEditor.tab.modelBaseSt === StType["TableAndMatrix::CustomizeTable::InstanceTable"] && !classifiers.length) {
        app.$notify({
          type: 'error',
          title: '提示',
          message: '实例表没有选择分类器'
        });
        return;
      }
      const res = await ImportExcelToTable.popup({
        projectId: tableEditor.projectId,
        modelId: tableEditor.modelId,
        modelBaseSt: tableEditor.tab.modelBaseSt as string,
        showUnit: tableEditor.toolbarModel.showColumnUnit,
        classifiers: JSON.stringify(classifiers) || '[]'
      });

      if (res.success) {
        tableEditor.refreshAll();
      }
    }

  }
}